package com.shenzhen.horselive.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.DashPathEffect;
import android.os.Bundle;
import android.view.View;

import com.bumptech.glide.load.DecodeFormat;
import com.flqy.baselibrary.utils.ScreenUtil;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.FenZhiDuiBean;
import com.shenzhen.horselive.beans.UserAcountBean;
import com.shenzhen.horselive.beans.ZouShiBean;
import com.shenzhen.horselive.databinding.ActivityFenZhiValueBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;
import com.shenzhen.horselive.presenter.ViewObserver;
import com.shenzhen.horselive.utils.A;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 峰值价值
 */
public class FenZhiValueActivity extends AppBarActivity {

    ActivityFenZhiValueBinding  binding;
    List<Entry>  list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding =ActivityFenZhiValueBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setStyleDark();
        setTitle("蜂值价值");
        setMenuText("明细");
        initViews();
        initData();
    }
    XAxis  xAxis;
    YAxis  yAxis;
    private void initViews() {
     binding.lineChart.getDescription().setEnabled(false);
     binding.lineChart.setDrawBorders(false);
      xAxis = binding.lineChart.getXAxis();
     xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//位置：
        xAxis.enableGridDashedLine(10f, 10f, 0f);
        //取消右边的轴
        binding.lineChart.getAxisRight().setEnabled(false);
        //取消边框
        //y轴的设置
        yAxis =  binding.lineChart.getAxisLeft();
        yAxis.setAxisMinimum(0.00f);
//        yAxis.setAxisMaximum(1.00f);
        yAxis.setGranularity(0.25f);
        yAxis.enableGridDashedLine(10f, 10f, 0f);
        binding.btGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FenZhiTransferOutActivity.start(FenZhiValueActivity.this,1);
            }
        });
    }

    private void initData() {
        //获取峰值的价值个数量
         getAcountForType(1);
         //获取走势
        getHistroy();
        //峰值兑换比例昨天和今天
        getDui();
    }

    private void getDui() {
        ApiFactory.getApiService().fenzhidui(User.get().getMyInfo().getToken())
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<FenZhiDuiBean>(this) {
                    @Override
                    protected void onNextInActive(FenZhiDuiBean fenZhiDuiBean) {
                        if (fenZhiDuiBean.isSuccess())
                        {
                            binding.tvTodayPrice.setText("¥"+formatFuckingNumber(fenZhiDuiBean.getData().getCurrentRatio()+""));
                            binding.tvYestodayPrice.setText("¥"+formatFuckingNumber(fenZhiDuiBean.getData().getYesterdayRatio()+""));
                         double  zhanguf =((fenZhiDuiBean.getData().getCurrentRatio()-fenZhiDuiBean.getData().getYesterdayRatio())/fenZhiDuiBean.getData().getYesterdayRatio())*100;
                         String  fudu =  formatFuckingNumber(zhanguf+"");
                         if (zhanguf>0)
                         binding.tvZhangfu.setText("涨幅+"+fudu+"%");
                         else
                         binding.tvZhangfu.setText("-"+fudu+"%");
                        }
                    }
                });
    }

    private void getHistroy() {
        ApiFactory.getApiService().zoushi(User.get().getMyInfo().getToken(),"2")
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<ZouShiBean>(this) {
                    @Override
                    protected void onNextInActive(ZouShiBean zouShiBean) {
                        if (zouShiBean.isSuccess())
                        {
                            //日期
                            List<String>  listDate =new ArrayList<>();

                            listDate.add(zouShiBean.getData().get_$6().getCreateTime().split(" ")[0]);
                            listDate.add(zouShiBean.getData().get_$5().getCreateTime().split(" ")[0]);
                            listDate.add(zouShiBean.getData().get_$4().getCreateTime().split(" ")[0]);
                            listDate.add(zouShiBean.getData().get_$3().getCreateTime().split(" ")[0]);
                            listDate.add(zouShiBean.getData().get_$2().getCreateTime().split(" ")[0]);
                            listDate.add(zouShiBean.getData().get_$1().getCreateTime().split(" ")[0]);
                            listDate.add(zouShiBean.getData().get_$0().getCreateTime().split(" ")[0]);
                            xAxis.setValueFormatter(new ValueFormatter() {
                                @Override
                                public String getFormattedValue(float value) {
                                    String  string
                                             = listDate.get((int) value);
                                    return string.split("-")[1]+"."+string.split("-")[2];

                                }
                            });
                            list.add(new Entry(0,Float.parseFloat(zouShiBean.getData().get_$6().getAfterRatio())));
                            list.add(new Entry(1,Float.parseFloat(zouShiBean.getData().get_$5().getAfterRatio())));
                            list.add(new Entry(2,Float.parseFloat(zouShiBean.getData().get_$4().getAfterRatio())));
                            list.add(new Entry(3,Float.parseFloat(zouShiBean.getData().get_$3().getAfterRatio())));
                            list.add(new Entry(4,Float.parseFloat(zouShiBean.getData().get_$2().getAfterRatio())));
                            list.add(new Entry(5,Float.parseFloat(zouShiBean.getData().get_$1().getAfterRatio())));
                            list.add(new Entry(6,Float.parseFloat(zouShiBean.getData().get_$0().getAfterRatio())));
                            //一个LineDataSet就是一条线
                            LineDataSet lineDataSet = new LineDataSet(list, "");
                            lineDataSet.setColor(getResources().getColor(R.color.red_common));
                            lineDataSet.setLineWidth(2);
                            lineDataSet.setDrawFilled(true);
                            lineDataSet.setDrawCircles(false);
                            lineDataSet.setFillDrawable(getResources().getDrawable(R.drawable.chat_fill));
                            LineData data = new LineData(lineDataSet);
                            binding.lineChart.setData(data);
                            binding.lineChart.invalidate();
                        }
                    }
                });

    }

    //帐户类型: 0:人民币帐户 ,1:蜂值帐户 ,2:红包股帐户 ,3:金卡帐户
    private void getAcountForType(int i) {
        ApiFactory.getApiService().accountTypes(User.get().getMyInfo().getToken(),i+"")
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<UserAcountBean>(this) {
                    @Override
                    protected void onNextInActive(UserAcountBean userAcountBean) {
                       if (userAcountBean.isSuccess())
                       {
                           binding.tvValue.setText("¥"+formatFuckingNumber(userAcountBean.getData().getValue()));
                           binding.tvNum.setText(formatFuckingNumber(userAcountBean.getData().getAmount()));

                           double donjie =Double.parseDouble(userAcountBean.getData().getFreezeAmount())/Double.parseDouble(userAcountBean.getData().getRatio());
                           binding.tvValueDon.setText("¥"+formatFuckingNumber(userAcountBean.getData().getFreezeAmount()));
                           binding.tvNumDon.setText(formatFuckingNumber(donjie+""));


                       }
                    }
                });

    }


    private float  chengTen(String  number)
    {
        return (float) (Double.parseDouble(number)*1000000000);

    }

    @Override
    protected void onActionMenuViewClick() {
        super.onActionMenuViewClick();
        //峰值明细
       FenZhiDetailsActivity.start(this,1);
    }
}